package com.plian.system.service.sys.grouprole;

import com.plian.Tools.TokenUtil;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.dao.sys.fun.PerGroupDao;
import com.plian.system.dao.sys.grouprole.GroupRoleDao;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class GroupRoleSercice {
    @Autowired
    GroupRoleDao groupRoleDao;
    @Autowired
    PerGroupDao perGroupDao;
    /**
     * 批量保存集团-账号
     * @param pd
     * @return
     */
    public ObjResult saveAll(PageData pd){
        try {
            List<String> userIds= (List<String>) pd.get("userIds");
            List<String> groupIds= (List<String>) pd.get("groupIds");
            List<PageData> list=new ArrayList<>();
            groupRoleDao.del_PerUserOrg(userIds);
            if(groupIds!=null && groupIds.size()>0){
                for (String userId : userIds) {
                    for (String groupId : groupIds) {
                        PageData p = new PageData();
                        p.put("id", UuidUtil.get32UUID());
                        p.put("userId", userId);
                        p.put("groupId", groupId);
                        list.add(p);
                    }
                }
                groupRoleDao.PerUserConfig(list);
            }
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            return  objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }

    /**
     * 配置当前账号下的角色
     * @param pd
     * @return
     */
    public ListResult list(PageData pd){
        //前端传来的账号id
        String userId=pd.getString("userId");
        try {
            //查询所有角色
           List<PageData> all= perGroupDao.listAll();
           //根据账号id查询勾选的权限
            List<PageData> list= groupRoleDao.queryPerUser(userId);
            if(list!=null && list.size()>0) {
                for (PageData p : all) {
                    boolean check=false;
                    for (PageData p1 : list) {
                        if(p.getString("id").equals(p1.getString("groupId"))){
                            check=true;
                            break;
                        }
                    }
                    p.put("check",check);
                }
            }
            ListResult listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(all);
            return listResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
        }

    public ListResult thisUserList() {
        //前端传来的账号id
        String userId= TokenUtil.getTokenUserId();
        //根据账号id查询勾选的权限
        try {
            List<PageData> list= groupRoleDao.queryPerUser(userId);
            ListResult listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(list);
            return listResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }

    /**
     * 获取集团
     * @return
     */
    public ListResult getRole(){
        try {
            List<PageData> l=groupRoleDao.getRole();
            ListResult listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(l);
            return listResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }

}
